package com.earogya.logic.medical_records;

import java.io.IOException;
import java.util.Calendar;

import org.apache.struts.ehr.medical_records.model.TransferToAnotherHospital;
import org.apache.struts.ehr.medical_records.model.TransferToAnotherWard;

import ca.uhn.hl7v2.model.DataTypeException;
import ca.uhn.hl7v2.model.v251.message.ADT_A02;
import ca.uhn.hl7v2.HL7Exception;

public class CreateADTA02 {

	private TransferToAnotherWard transfer_to_another_ward;
	private TransferToAnotherHospital transfer_to_another_hospital;
	
	private ADT_A02 adt;
	
	public CreateADTA02(TransferToAnotherWard transfer){
		this.adt=new ADT_A02();
		this.transfer_to_another_ward=transfer;
	}
	
	public CreateADTA02(TransferToAnotherHospital transfer){
		this.adt=new ADT_A02();
		this.transfer_to_another_hospital=transfer;
	}
	
	public String createMessage(String sendingFacility, String transferType) throws HL7Exception, IOException{
		this.addMessageHeader(sendingFacility);
		if(transferType.equals("ward")){
			this.addPatientDetails(transfer_to_another_ward.getPatientId());
			this.transferToAnotherWard();
		}
		else if(transferType.equals("hospital")){
			this.addPatientDetails(String.valueOf(transfer_to_another_hospital.getPatientId()));
			this.transferToAnotherHospital();
		}
		else{
			HL7Exception ex=new HL7Exception("Incorrect Transfer type. Should be \"ward\" or \"hospital\"");
			adt.generateACK("AE", ex);
		}
		return adt.getMessage().encode();
	}
	
	private void addMessageHeader(String sendingFacility) throws DataTypeException{
		adt.getMSH().getFieldSeparator().setValue("|");
		adt.getMSH().getEncodingCharacters().setValue("^~\\&");
		adt.getMSH().getDateTimeOfMessage().getTime().setValue(Calendar.getInstance().getTime());
		adt.getMSH().getMessageType().getMsg1_MessageCode().setValue("ADT");
		adt.getMSH().getMessageType().getTriggerEvent().setValue("A02");
		adt.getMSH().getSendingApplication().getUniversalID().setValue("EAFE");
		adt.getMSH().getVersionID().getVersionID().setValue("2.5.1");
		adt.getMSH().getSendingFacility().getUniversalID().setValue(sendingFacility);
		adt.getMSH().getReceivingApplication().getUniversalID().setValue("PVS");
		adt.getMSH().getReceivingFacility().getUniversalID().setValue(sendingFacility);
	}
	
	private void addPatientDetails(String patient_id) throws DataTypeException{
		adt.getPID().getPatientID().getIDNumber().setValue(patient_id);
	}
	
	private void transferToAnotherWard() throws DataTypeException{
		adt.getPV1().getPriorPatientLocation().getFacility().getUniversalID().setValue(transfer_to_another_ward.getHospital());
		adt.getPV1().getPriorPatientLocation().getRoom().setValue(transfer_to_another_ward.getFromWardNo());
		adt.getPV1().getPriorPatientLocation().getBed().setValue(transfer_to_another_ward.getFromBedNo());
		adt.getPV1().getPriorPatientLocation().getLocationDescription().setValue("CSTH");
		adt.getPV1().getAssignedPatientLocation().getRoom().setValue(transfer_to_another_ward.getToWardNo());
		adt.getPV1().getAssignedPatientLocation().getBed().setValue(transfer_to_another_ward.getToBedNo());
		adt.getPV1().getAssignedPatientLocation().getLocationDescription().setValue("CSTH");
		adt.getPV1().getConsultingDoctor(0).getIDNumber().setValue(transfer_to_another_ward.getTransferringOfficerId());
		adt.getPV1().getDischargeDateTime(0).getTime().setValue(transfer_to_another_ward.getTransferTime());
		adt.getPV2().getTransferReason().getText().setValue(transfer_to_another_ward.getReason());
		adt.getPV2().getPatientConditionCode().getAlternateText().setValue(transfer_to_another_ward.getTreatmentSuggested());
	}
	
	private void transferToAnotherHospital() throws DataTypeException{
		adt.getPV1().getPriorPatientLocation().getFacility().getUniversalID().setValue(transfer_to_another_hospital.getFromHospital());
		adt.getPV1().getPriorPatientLocation().getBed().setValue(transfer_to_another_hospital.getFromBedNo());
		adt.getPV1().getPriorPatientLocation().getRoom().setValue(transfer_to_another_hospital.getFromWardNo());
		adt.getPV1().getAssignedPatientLocation().getFacility().getUniversalID().setValue(transfer_to_another_hospital.getToHospital());
		adt.getPV2().getPatientConditionCode().getAlternateText().setValue(transfer_to_another_hospital.getTreatmentSuggested());
		adt.getPV1().getConsultingDoctor(0).getIDNumber().setValue(String.valueOf(transfer_to_another_hospital.getTransferringOfficerId()));
		adt.getPV2().getTransferReason().getText().setValue(transfer_to_another_hospital.getReason());
		adt.getPV2().getModeOfArrivalCode().getAlternateText().setValue(String.valueOf(transfer_to_another_hospital.getAmbulanceRequestId()));
		adt.getPV2().getPatientStatusCode().setValue(transfer_to_another_hospital.getStatus());
		adt.getPV1().getDischargeDateTime(0).getTime().setValue(transfer_to_another_hospital.getTransferTime());
	}
	
}
